class Index {
    gradeInfoData = []
    gradeData = {}
    init = () => {
        this.getGradeInfoData();
        this.initGradeDropdownMenu();
    }

    getGradeInfoData = () => {
        $.ajaxSettings.async = false;
        $.get("/resource/data/grade/gradeInfo.json", (result) => {
            this.gradeInfoData = result.data;
        });
    }

    selectGrade = (s) => {
        let gradeDict = JSON.parse(JSON.stringify(this.gradeInfoData));
        for (let key in gradeDict) {
            const code = gradeDict[key].code;
            if (s == code) {
                this.gradeData = gradeDict[key];
            }
        }
        this.initGrade();
    }

    initGradeDropdownMenu = () => {
        $("#class-dropdown-menu").html("");
        let gradeDict = JSON.parse(JSON.stringify(this.gradeInfoData));
        for (let key in gradeDict) {
            const name = gradeDict[key].name;
            const code = gradeDict[key].code;
            if (name.length > 0) {
                $("#class-dropdown-menu").append('<li><a class="dropdown-item" href="#" onclick="selectGrade(' + code + ')">' + name + '</a></li>');
            } else {
                $("#class-dropdown-menu").append('<li><hr class="dropdown-divider"></li>');
            }
        }
    }

    initGrade = (code) => {
        let gradeData = JSON.parse(JSON.stringify(this.gradeData));
        console.log(gradeData)
        if (gradeData) {
            $("#card-title").html(gradeData.name);
            const code = gradeData.code;
            $.ajaxSettings.async = false;
            $.get("/resource/data/grade/" + code + ".json", function (result, status) {
                let hzArr = [];
                const data = result.data;
                for (let str of data) {
                    if ((/[\u4e00-\u9fa5]/).test(str)) {
                        hzArr.push(str);
                    }
                }
                $("#gradeWordData").html(hzArr.join(","));
                $("#links").html(
                    `
                        <a href="/page/zitie.html?code=` + code + `" class="btn btn-success btn-sm">字帖</a>
                        <a href="/page/bishun.html?code=` + code + `" class="btn btn-primary btn-sm">练字</a>
                    `
                );
                app.gradeWordData = hzArr;
            });
        }
    }

    getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }

}
